Chuyển đổi các hệ cơ số C#
- Form1.cs
- project /
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace WindowsFormsApplication1
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 private void label1_Click(object sender, EventArgs e)
20 {
21
22 }
23
24 /* ======================== ĐỔI TỪ DEC SANG BIN =================================== */
25
26 private void button1_Click(object sender, EventArgs e)
27 {
28 try
29 {
30 string a = textBox1.Text;
31 string[] b = a.Split('.'); //cat chuoi
32
33 /* ############## Doi phan nguyen ############*/
34
35 Int32 n = Convert.ToInt32(b[0]);
36 Int32 i = 0;
37 double d, s1 = 0;
38 while (n > 0)
39 {
40 d = n % 2;
41 n = n / 2;
42 s1 = s1 + d * Math.Pow(10, i);//lay gia tri bin
43 i++;
44 };
45
46 /* ############## Doi phan le ############*/
47
48 string f = "0." + b[1];
49 string s2 = "";
50 double g = Convert.ToDouble(f);
51 Int32 w;
52 while (g != 0)
53 {
54 g = g * 2;
55 w = Convert.ToInt32(g);//phan nguyen co lam tron
56 if (w <= g)//lam tron < g
57 {
58 s2 = s2 + Convert.ToString(w);
59 g = g - w;
60 }
61 else //lam tron > g
62 {
63 s2 = s2 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
64 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
65 }
66
67 };
68 textBox2.Text = Convert.ToString(s1) + "." + s2;
69 }
70
71 catch
72 {
73 Int32 n = Int32.Parse(textBox1.Text);//chuyen chuoi trong textBox1 thanh kieu so nguyen
74 double s = 0, i = 0, k;// s phai la kieu double vi chua ham mu
75 while (n > 0)
76 {
77 k = n % 2;
78 n = n / 2;
79 s = s + k * Math.Pow(10, i);// dao nguoc so k vua tinh thanh bin can tim
80 i++;
81 };
82 string b = Convert.ToString(s);//chuyen s ve kieu string
83 textBox2.Text = b;
84 }
85 }
86
87 /* ================================ ĐỔI TỪ DEC SANG OCT ==================================== */
88
89 private void button2_Click(object sender, EventArgs e)
90 {
91 try
92 {
93 string a = textBox1.Text;
94 string[] P = a.Split('.'); //cat chuoi
95 Int32 n = Convert.ToInt32(P[0]);
96
97 /* ############## Doi phan nguyen ############ */
98
99 Int32 i = 0;
100 double d, s1 = 0;
101 while (n > 0)
102 {
103 d = n % 8;
104 n = n / 8;
105 s1 = s1 + d * Math.Pow(10, i);//lay gia tri bin
106 i++;
107 };
108
109 /* ########## Doi phan le ########### */
110
111 string f = "0." + P[1];
112 string s2 = "";
113 double g = Convert.ToDouble(f);
114
115 Int32 w;
116 while (g != 0)//vong lap ket thuc khi phan le =0
117 {
118 g = g * 8;
119 w = Convert.ToInt32(g);//phan nguyen co lam tron
120 if (w <= g)//lam tron < g
121 {
122 s2 = s2 + Convert.ToString(w);
123 g = g - w;
124 }
125 else //lam tron > g
126 {
127 s2 = s2 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
128 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
129 }
130
131 };
132 textBox2.Text = Convert.ToString(s1) + "." + s2;
133
134 }
135 catch
136 {
137 Int32 n = Int32.Parse(textBox1.Text);//chuyen chuoi trong textBox1 thanh kieu so nguyen
138 double s = 0, i = 0, k;// s phai la kieu double vi chua ham mu
139 while (n > 0)
140 {
141 k = n % 8;
142 n = n / 8;
143 s = s + k * Math.Pow(10, i);// dao nguoc so k vua tinh thanh oct can tim
144 i++;
145 };
146 string b = Convert.ToString(s);//chuyen s ve kieu string
147 textBox2.Text = b;
148 }
149
150
151 }
152
153 /* ==================================== ĐỔI TỪ DEC SANG HEX ====================================== */
154
155 private void button3_Click(object sender, EventArgs e)
156 {
157 try
158 {
159 string m = textBox1.Text;
160 string[] P = m.Split('.'); //cat chuoi
161
162 /* ########### Doi phan nguyen ########### */
163
164 Int32 n1 = Convert.ToInt32(P[0]);
165 string[] A; // vi he thap luc phan la chuoi
166 A = new string[100];
167 string s1 = "";
168
169 Int32 i = 0;
170 while (n1 > 0)
171 {
172
173 A[i] = Convert.ToString(n1 % 16); // vi a[i] thuoc kieu string
174 n1 = n1 / 16;
175 i++;
176 };
177
178 //in ra he thap luc phan
179 for (Int32 j = i - 1; j >= 0; j--)// lay j giam de dc he thap luc phan can tim
180 {
181 if (A[j] == "10") A[j] = "A";//a[j] la kieu chuoi
182 if (A[j] == "11") A[j] = "B";
183 if (A[j] == "12") A[j] = "C";
184 if (A[j] == "13") A[j] = "D";
185 if (A[j] == "14") A[j] = "E";
186 if (A[j] == "15") A[j] = "F";
187
188 s1 = s1 + A[j];
189 }
190
191 textBox2.Text = s1;
192
193 /* ########### Doi phan le ########### */
194
195 string f = "0." + P[1];
196 string s2 = "";
197 double g = Convert.ToDouble(f);
198
199 Int64 w;
200
201 while (g != 0)//vong lap ket thuc khi phan le =0
202 {
203 g = g * 16;
204 w = Convert.ToInt64(g);//phan nguyen co lam tron
205 if (w <= g)//lam tron < g
206 {
207
208 if (w == 10) s2 = s2 + "A";
209 else if (w == 11) s2 = s2 + "B";
210 else if (w == 12) s2 = s2 + "C";
211 else if (w == 13) s2 = s2 + "D";
212 else if (w == 14) s2 = s2 + "E";
213 else if (w == 15) s2 = s2 + "F";
214 else s2 = s2 + Convert.ToString(w);
215 g = g - w;
216
217 }
218 else //lam tron > g
219 {
220 if (w-1 == 10) s2 = s2 + "A";
221 else if (w-1 == 11) s2 = s2 + "B";
222 else if (w-1 == 12) s2 = s2 + "C";
223 else if (w-1 == 13) s2 = s2 + "D";
224 else if (w-1 == 14) s2 = s2 + "E";
225 else if (w - 1 == 15) s2 = s2 + "F";
226 else s2 = s2 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
227 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
228
229 }
230 textBox2.Text = Convert.ToString(s1) + "." + s2;
231 };
232
233 }
234
235 catch
236
237 {
238 string[] a; // vi he thap luc phan la chuoi
239 a = new string[50];
240 string b = "";
241
242 Int32 n = Int32.Parse(textBox1.Text);
243 Int32 i = 0;
244 while (n > 0)
245 {
246
247 a[i] = Convert.ToString(n % 16); // vi a[i] thuoc kieu string
248 n = n / 16;
249 i++;
250 };
251
252 //in ra he thap luc phan
253 for (Int32 j = i - 1; j >= 0; j--)// lay j giam de dc he thap luc phan can tim
254 {
255 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
256 if (a[j] == "11") a[j] = "B";
257 if (a[j] == "12") a[j] = "C";
258 if (a[j] == "13") a[j] = "D";
259 if (a[j] == "14") a[j] = "E";
260 if (a[j] == "15") a[j] = "F";
261
262 b = b + a[j];
263 }
264
265 textBox2.Text = b;
266 }
267
268 }
269
270 /* ================================= ĐỔI TỪ BIN SANG DEC ========================================= */
271
272 private void button4_Click(object sender, EventArgs e)
273 {
274 try
275 {
276
277 string a = textBox1.Text;
278 string[] P = a.Split('.');
279
280 /* ############# doi phan nguyen ############### */
281
282 Int32 n1 = Convert.ToInt32(P[0]);
283 double s1 = 0, i = 0, k = 0;
284 while (n1 > 0)
285 {
286 k = n1 % 10; // lay ra phan tu cuoi cung cua bin
287 n1 = n1 / 10;
288 s1 = s1 + k * Math.Pow(2, i);
289 i++;
290 };
291
292
293 /* ############## Doi phan le ############## */
294
295 string n2 = P[1];//phan sau dau cham
296 Int32 j = n2.Length;//dem chieu dai chuoi
297
298 Int32 m = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
299 double s2 = 0;
300 Int32 t = 0;
301 while (j > 0)
302 {
303 t = m % 10;
304 m = m / 10;
305 s2 = s2 + t * Math.Pow(2, (-1) * j);
306 j--;
307
308 }
309 double z = s1 + s2;
310 textBox2.Text = Convert.ToString(z);
311 }
312
313 catch
314 {
315 Int32 n = Int32.Parse(textBox1.Text);
316 double s = 0, i = 0, k = 0;
317 while (n > 0)
318 {
319 k = n % 10; // lay ra phan tu cuoi cung cua bin
320 n = n / 10;
321 s = s + k * Math.Pow(2, i);
322 i++;
323 };
324 string b = Convert.ToString(s);
325 textBox2.Text = b;
326 }
327
328 }
329
330 /* ==================================== ĐỔI TỪ BIN SANG OCT ======================================= */
331
332 private void button5_Click(object sender, EventArgs e)
333 {
334 try
335 {
336 string a = textBox1.Text;
337 string[] P = a.Split('.');
338
339 /* ############ doi phan nguyen ############ */
340
341 // DOI TU BIN SANG DEC
342
343 Int32 n1 = Convert.ToInt32(P[0]);
344 double s1 = 0, i = 0, k = 0;
345 while (n1 > 0)
346 {
347 k = n1 % 10; // lay ra phan tu cuoi cung cua bin
348 n1 = n1 / 10;
349 s1 = s1 + k * Math.Pow(2, i);
350 i++;
351 };
352
353 //DOI TU DEC SANG OCT
354
355 Int32 n = Convert.ToInt32(s1);
356 double s2 = 0,l=0;// s phai la kieu double vi chua ham mu
357 while (n > 0)
358 {
359 k = n % 8;
360 n = n / 8;
361 s2 = s2 + k * Math.Pow(10, l);// dao nguoc so k vua tinh thanh oct can tim
362 l++;
363 };
364
365 /* ############ doi phan le ############ */
366
367 // DOI TU BIN SANG DEC
368
369 string n2 = P[1];//phan sau dau cham
370 Int32 j = n2.Length;//dem chieu dai chuoi
371
372 Int32 m = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
373 double s3 = 0;
374 Int32 t = 0;
375 while (j > 0)
376 {
377 t = m % 10;
378 m = m / 10;
379 s3 = s3 + t * Math.Pow(2, (-1) * j);
380 j--;
381
382 }
383 //DOI TU DEC SANG OCT
384
385 string s4 = "";
386 double g = s3;
387 Int32 w;
388 while (g != 0)//vong lap ket thuc khi phan le =0
389 {
390 g = g * 8;
391 w = Convert.ToInt32(g);//phan nguyen co lam tron
392 if (w <= g)//lam tron < g
393 {
394 s4 = s4 + Convert.ToString(w);
395 g = g - w;
396 }
397 else //lam tron > g
398 {
399 s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
400 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
401 }
402 };
403 textBox2.Text = Convert.ToString(s2)+ '.'+s4;
404 }
405 catch
406 {
407 //DOI TU BIN SANG DEC
408 string a = textBox1.Text;
409 Int32 n1 = Convert.ToInt32(a);
410 double s1 = 0, i = 0, k = 0;
411 while (n1 > 0)
412 {
413 k = n1 % 10; // lay ra phan tu cuoi cung cua bin
414 n1 = n1 / 10;
415 s1 = s1 + k * Math.Pow(2, i);
416 i++;
417 };
418
419 //DOI TU DEC SANG OCT
420
421 Int32 n = Convert.ToInt32(s1);
422 double s2 = 0, l = 0;// s phai la kieu double vi chua ham mu
423 while (n > 0)
424 {
425 k = n % 8;
426 n = n / 8;
427 s2 = s2 + k * Math.Pow(10, l);// dao nguoc so k vua tinh thanh oct can tim
428 l++;
429 };
430 textBox2.Text = Convert.ToString(s2);
431 }
432 }
433
434 /* ================================== ĐỔI TỪ BIN SANG HEX ========================================== */
435
436 private void button6_Click(object sender, EventArgs e)
437 {
438 try
439 {
440 string c = textBox1.Text;
441 string[] P = c.Split('.');
442
443 /* ############## doi phan nguyen ############# */
444
445 // DOI TU BIN SANG DEC
446
447 Int32 n = Convert.ToInt32(P[0]);
448 double s1 = 0, i = 0, k = 0;
449 while (n > 0)
450 {
451 k = n % 10; //lay ra phan tu cuoi cung cua bin
452 n = n / 10;
453 s1 = s1 + k * Math.Pow(2,i);
454 i++;
455 };
456
457 // DOI TU DEC SANG HEX
458
459 string[] a;
460 a = new string[50];
461 string s2 = "";
462
463 /* lay so thap phan s vua tim doi thanh kieu so nguyen
464 roi chuyen thanh he thap luc phan */
465
466 Int32 m = Convert.ToInt32(s1);
467 int q= 0;
468 while (m > 0)
469 {
470
471 a[q] = Convert.ToString(m % 16); // vi a[q] thuoc kieu string
472 m = m / 16;
473 q++;
474 };
475 //in ra he thap luc phan
476 for (Int32 j = q - 1; j >= 0; j--)
477 {
478 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
479 if (a[j] == "11") a[j] = "B";
480 if (a[j] == "12") a[j] = "C";
481 if (a[j] == "13") a[j] = "D";
482 if (a[j] == "14") a[j] = "E";
483 if (a[j] == "15") a[j] = "F";
484
485 s2 = s2+ a[j];
486 }
487 /* ################### doi phan le ################ */
488
489 // DOI TU BIN SANG DEC
490
491 string n2 = P[1];//phan sau dau cham
492 Int32 l = n2.Length;//dem chieu dai chuoi
493
494 Int32 h = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
495 double s3 = 0;
496 Int32 t = 0;
497 while (l > 0)
498 {
499 t = h % 10;
500 h = h / 10;
501 s3 = s3 + t * Math.Pow(2, (-1) * l);
502 l--;
503
504 }
505 // DOI TU DEC SANG HEX
506
507 string s4 = "";
508 double g = s3;
509
510 Int64 w;
511
512 while (g != 0)//vong lap ket thuc khi phan le =0
513 {
514 g = g * 16;
515 w = Convert.ToInt64(g);//phan nguyen co lam tron
516 if (w <= g)//lam tron < g
517 {
518
519 if (w == 10) s4 = s4 + "A";
520 else if (w == 11) s4 = s4 + "B";
521 else if (w == 12) s4 = s4 + "C";
522 else if (w == 13) s4 = s4 + "D";
523 else if (w == 14) s4 = s4 + "E";
524 else if (w == 15) s4 = s4 + "F";
525 else s4 = s4 + Convert.ToString(w);
526 g = g - w;
527
528 }
529 else //lam tron > g
530 {
531 if (w-1 == 10) s4 = s4 + "A";
532 else if (w-1 == 11) s4 = s4 + "B";
533 else if (w-1 == 12) s4 = s4 + "C";
534 else if (w-1 == 13) s4 = s4 + "D";
535 else if (w-1 == 14) s4 = s4 + "E";
536 else if (w - 1 == 15) s4 = s4 + "F";
537 else s4= s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
538 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
539 }
540 }
541 textBox2.Text = Convert.ToString(s2) + "." + s4;
542
543 }
544 catch
545 {
546 // DOI TU BIN SANG DEC
547
548 Int32 n = Int32.Parse(textBox1.Text);
549 double s = 0, i = 0, k = 0;
550 while (n > 0)
551 {
552 k = n % 10; //lay ra phan tu cuoi cung cua bin
553 n = n / 10;
554 s = s + k * Math.Pow(2,i);
555 i++;
556 };
557
558 // DOI TU DEC SANG HEX
559
560 string[] a;
561 a = new string[50];
562 string b = "";
563
564 /* lay so thap phan s vua tim doi thanh kieu so nguyen
565 roi chuyen thanh he thap luc phan */
566
567 Int32 m = Convert.ToInt32(s);
568 int q= 0;
569 while (m > 0)
570 {
571
572 a[q] = Convert.ToString(m % 16); // vi a[q] thuoc kieu string
573 m = m / 16;
574 q++;
575 };
576 //in ra he thap luc phan
577 for (Int32 j = q - 1; j >= 0; j--)
578 {
579 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
580 if (a[j] == "11") a[j] = "B";
581 if (a[j] == "12") a[j] = "C";
582 if (a[j] == "13") a[j] = "D";
583 if (a[j] == "14") a[j] = "E";
584 if (a[j] == "15") a[j] = "F";
585
586 b = b + a[j];
587 }
588
589 textBox2.Text = b;
590 }
591 }
592
593 /* ========================================= ĐỔI TỪ OCT SANG DEC ============================================= */
594
595 private void button7_Click(object sender, EventArgs e)
596 {
597
598 try
599 {
600
601 string a = textBox1.Text;
602 string[] P = a.Split('.');
603
604 /* ############## doi phan nguyen ############ */
605
606 Int32 n1 = Convert.ToInt32(P[0]);
607 double s1 = 0, i = 0, k = 0;
608 while (n1 > 0)
609 {
610 k = n1 % 10; // lay ra phan tu cuoi cung cua bin
611 n1 = n1 / 10;
612 s1 = s1 + k * Math.Pow(8, i);
613 i++;
614 };
615
616
617 /* ############ Doi phan le ##################*/
618
619 string n2 = P[1];//phan sau dau cham
620 Int32 j = n2.Length;//dem chieu dai chuoi
621
622 Int32 m = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
623 double s2 = 0;
624 Int32 t = 0;
625 while (j > 0)
626 {
627 t = m % 10;
628 m = m / 10;
629 s2 = s2 + t * Math.Pow(8, (-1) * j);
630 j--;
631
632 }
633 double z = s1 + s2;
634 textBox2.Text = Convert.ToString(z);
635 }
636
637 catch
638 {
639
640
641
642 Int32 n = Int32.Parse(textBox1.Text);
643 double s = 0, i = 0, k = 0;
644 while (n > 0)
645 {
646 k = n % 10;// lay ra phan tu cuoi cung cua oct
647 n = n / 10;
648 s = s + k * Math.Pow(8, i);
649 i++;
650 };
651 string b = Convert.ToString(s);
652 textBox2.Text = b;
653 }
654 }
655
656
657 /* =================================== ĐỔI TỪ OCT SANG BIN ========================================*/
658
659 private void button8_Click(object sender, EventArgs e)
660 {
661 try
662 {
663 string a = textBox1.Text;
664 string[] P = a.Split('.');
665
666 /* ############## doi phan nguyen ###########*/
667
668 //DOI TU OCT SANG DEC
669
670 Int32 n = Convert.ToInt32(P[0]);
671 double s1 = 0, i = 0, k = 0, s2 = 0;
672 while (n > 0)
673 {
674 k = n % 10; // lay ra phan tu cuoi cung cua oct
675 n = n / 10;
676 s1 = s1 + k * Math.Pow(8, i);
677 i++;
678 };
679
680 //DOI TU DEC SANG BIN
681
682 Int32 q = Convert.ToInt32(s1);
683 i = 0;
684 while (q > 0)
685 {
686 k = q % 2;
687 q = q / 2;
688 s2 = s2 + k * Math.Pow(10, i); //doi bin k vua tinh ra nguoc lai de duoc bin can tim
689 i++;
690 };
691
692 /* ############## doi phan le ###########*/
693
694 //DOI TU OCT SANG DEC
695
696 string n2 = P[1];//phan sau dau cham
697 Int32 j = n2.Length;//dem chieu dai chuoi
698
699 Int32 m = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
700 double s3 = 0;
701 Int32 t = 0;
702 while (j > 0)
703 {
704 t = m % 10;
705 m = m / 10;
706 s3 = s3 + t * Math.Pow(8, (-1) * j);
707 j--;
708
709 }
710
711 //DOI TU DEC SANG BIN
712
713 string s4 = "";
714 double g = s3;
715 Int32 w;
716 while (g != 0)
717 {
718 g = g * 2;
719 w = Convert.ToInt32(g);//phan nguyen co lam tron
720 if (w <= g)//lam tron < g
721 {
722 s4 = s4 + Convert.ToString(w);
723 g = g - w;
724 }
725 else //lam tron > g
726 {
727 s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
728 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
729 }
730 };
731 textBox2.Text = Convert.ToString(s2) + '.' + s4;
732 }
733 catch
734 {
735 // DOI TU OCT SANG DEC
736
737 Int32 n = Int32.Parse(textBox1.Text);
738 double s1 = 0, i = 0, k = 0, s2 = 0;
739 while (n > 0)
740 {
741 k = n % 10; // lay ra phan tu cuoi cung cua oct
742 n = n / 10;
743 s1 = s1 + k * Math.Pow(8, i);
744 i++;
745 };
746
747 //DOI TU DEC SANG BIN
748
749 Int32 q = Convert.ToInt32(s1);
750 i = 0;
751 while (q > 0)
752 {
753 k = q % 2;
754 q = q / 2;
755 s2 = s2 + k * Math.Pow(10, i); //doi bin k vua tinh ra nguoc lai de duoc bin can tim
756 i++;
757 };
758 textBox2.Text = Convert.ToString(s2);
759 }
760 }
761
762 /* ===================================== ĐỔI TỪ OCT SANG HEX =============================================== */
763
764 private void button9_Click(object sender, EventArgs e)
765 {
766 try
767 {
768 string b = textBox1.Text;
769 string[] P = b.Split('.');
770
771 /* ############## doi phan nguyen ############ */
772
773 //DOI TU OCT SANG DEC
774
775 Int32 n = Convert.ToInt32(P[0]);
776 double s1 = 0, i = 0, k = 0;
777 while (n > 0)
778 {
779 k = n % 10;//lay ra phan tu cuoi cung cua oct
780 n = n / 10;
781 s1 = s1 + k * Math.Pow(8, i);
782 i++;
783 }
784
785 //DOI TU DEC SANG HEX
786
787 string[] a;
788 a = new string[100];
789 string s2 = "";
790
791 /* lay so thap phan s1 vua tim doi thanh kieu so nguyen
792 roi chuyen thanh he thap luc phan */
793
794 Int32 m = Convert.ToInt32(s1);
795 Int32 q = 0;
796 while (m > 0)
797 {
798 a[q] = Convert.ToString(m % 16);
799 m = m / 16;
800 q++;
801 }
802 //in he thap luc phan
803 for (Int32 j = q - 1; j >= 0; j--)
804 {
805 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
806 if (a[j] == "11") a[j] = "B";
807 if (a[j] == "12") a[j] = "C";
808 if (a[j] == "13") a[j] = "D";
809 if (a[j] == "14") a[j] = "E";
810 if (a[j] == "15") a[j] = "F";
811
812 s2 = s2 + a[j];
813 }
814 /* ############ Doi phan le ##################*/
815
816 //DOI TU OCT SANG DEC
817
818 string n2 = P[1];//phan sau dau cham
819 Int32 l = n2.Length;//dem chieu dai chuoi
820
821 Int32 m1 = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
822 double s3 = 0;
823 Int32 t = 0;
824 while (l > 0)
825 {
826 t = m1 % 10;
827 m1 = m1 / 10;
828 s3 = s3 + t * Math.Pow(8, (-1) * l);
829 l--;
830 }
831 //DOI TU DEC SANG HEC
832
833 string s4 = "";
834 double g = s3;
835
836 Int64 w;
837
838 while (g != 0)//vong lap ket thuc khi phan le =0
839 {
840 g = g * 16;
841 w = Convert.ToInt64(g);//phan nguyen co lam tron
842 if (w <= g)//lam tron < g
843 {
844
845 if (w == 10) s4 = s4 + "A";
846 else if (w == 11) s4 = s4 + "B";
847 else if (w == 12) s4 = s4 + "C";
848 else if (w == 13) s4 = s4 + "D";
849 else if (w == 14) s4 = s4 + "E";
850 else if (w == 15) s4 = s4 + "F";
851 else s4 = s4 + Convert.ToString(w);
852 g = g - w;
853
854 }
855 else //lam tron > g
856 {
857 if (w - 1 == 10) s4 = s4 + "A";
858 else if (w - 1 == 11) s4 = s4 + "B";
859 else if (w - 1 == 12) s4 = s4 + "C";
860 else if (w - 1 == 13) s4 = s4 + "D";
861 else if (w - 1 == 14) s4 = s4 + "E";
862 else if (w - 1 == 15) s4 = s4 + "F";
863 else s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
864 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
865
866 }
867 }
868 textBox2.Text = Convert.ToString(s2) + "." + s4;
869 }
870
871 catch
872 {
873 //DOI TU OCT SANG DEC
874
875 Int32 n = Int32.Parse(textBox1.Text);
876 double s = 0, i = 0, k = 0;
877 while (n > 0)
878 {
879 k = n % 10;//lay ra phan tu cuoi cung cua oct
880 n = n / 10;
881 s = s + k * Math.Pow(8, i);
882 i++;
883 }
884
885 //DOI TU DEC SANG HEC
886
887 string[] a;
888 a = new string[50];
889 string b = "";
890
891 /* lay so thap phan s vua tim doi thanh kieu so nguyen
892 roi chuyen thanh he thap luc phan */
893
894 Int32 m = Convert.ToInt32(s);
895 Int32 q = 0;
896 while (m > 0)
897 {
898 a[q] = Convert.ToString(m % 16);
899 m = m / 16;
900 q++;
901 }
902 //in he thap luc phan
903 for (Int32 j = q - 1; j >= 0; j--)
904 {
905 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
906 if (a[j] == "11") a[j] = "B";
907 if (a[j] == "12") a[j] = "C";
908 if (a[j] == "13") a[j] = "D";
909 if (a[j] == "14") a[j] = "E";
910 if (a[j] == "15") a[j] = "F";
911
912 b = b + a[j];
913 }
914
915 textBox2.Text = b;
916 }
917
918 }
919 /* ================================== ĐỔI TỪ HEX SANG DEC ========================================= */
920
921 private void button10_Click(object sender, EventArgs e)
922 {
923 try
924 {
925 string c = textBox1.Text;
926 string[] P = c.Split('.');
927
928 /* ############## doi phan nguyen ############# */
929
930 string n1=P[0];
931 string[] a;
932 string[] b;
933 a = new string[100];
934 b = new string[100];
935 Int32 i = n1.Length - 1;// i bang chieu dai chuoi
936 double s1 = 0, j = 0;
937 while (i >= 0)
938 {
939 a[i] = n1.Substring(i,1);// cat chuoi co do dai bang 1
940 if (a[i] == "A") b[i] = "10";
941 else if (a[i] == "B") b[i] = "11";
942 else if (a[i] == "C") b[i] = "12";
943 else if (a[i] == "D") b[i] = "13";
944 else if (a[i] == "E") b[i] = "14";
945 else if (a[i] == "F") b[i] = "15";
946 else b[i] = a[i];
947 s1 = s1 + Convert.ToDouble(b[i]) * Math.Pow(16, j);
948 i--;//chieu dai chuoi giam
949 j++;//so mu 16 tang
950
951 }
952 /* ############## doi phan le ############# */
953
954 string n2 = P[1];
955 string[] A;
956 string[] B;
957 A = new string[100];
958 B= new string[100];
959 Int32 l = n2.Length - 1;// l bang chieu dai chuoi
960 double s2 = 0;
961 while (l >= 0)
962 {
963 A[l] = n2.Substring(l, 1);// cat chuoi co do dai bang 1
964 if (A[l] == "A") B[l] = "10";
965 else if (A[l] == "B") B[l] = "11";
966 else if (A[l] == "C") B[l] = "12";
967 else if (A[l] == "D") B[l] = "13";
968 else if (A[l] == "E") B[l] = "14";
969 else if (A[l] == "F") B[l] = "15";
970 else B[l] = A[l];
971 s2 = s2 + Convert.ToDouble(B[l]) * Math.Pow(16, (-1) * (l + 1));
972 l--;//chieu dai chuoi giam
973 }
974 double s3 = s1 + s2;
975 textBox2.Text = Convert.ToString(s3);
976 }
977
978 catch
979 {
980 string n = textBox1.Text;
981 string[] a;
982 string[] b;
983 a = new string[100];
984 b = new string[100];
985 Int32 i = n.Length - 1;// i bang chieu dai chuoi
986 double s = 0, j = 0;
987 while (i >= 0)
988 {
989 a[i] = n.Substring(i, 1);// cat chuoi co do dai bang 1
990 if (a[i] == "A") b[i] = "10";
991 else if (a[i] == "B") b[i] = "11";
992 else if (a[i] == "C") b[i] = "12";
993 else if (a[i] == "D") b[i] = "13";
994 else if (a[i] == "E") b[i] = "14";
995 else if (a[i] == "F") b[i] = "15";
996 else b[i] = a[i];
997 s = s + Convert.ToDouble(b[i]) * Math.Pow(16, j);
998 i--;//chieu dai chuoi giam
999 j++;//so mu 16 tang
1000
1001 }
1002 textBox2.Text = Convert.ToString(s);
1003 }
1004 }
1005
1006 /* ======================================= ĐỔI TỪ HEX SANG BIN ===================================== */
1007
1008 private void button11_Click(object sender, EventArgs e)
1009 {
1010 try
1011 {
1012 string c = textBox1.Text;
1013 string[] P = c.Split('.');
1014
1015 /* ############## doi phan nguyen ############# */
1016
1017 // DOI TU HEX SANG DEC
1018
1019 string n1 = P[0];
1020 string[] a;
1021 string[] b;
1022 a = new string[100];
1023 b = new string[100];
1024 Int32 i = n1.Length - 1;// i bang chieu dai chuoi
1025 double s1 = 0, j = 0;
1026 while (i >= 0)
1027 {
1028 a[i] = n1.Substring(i, 1);// cat chuoi co do dai bang 1
1029 if (a[i] == "A") b[i] = "10";
1030 else if (a[i] == "B") b[i] = "11";
1031 else if (a[i] == "C") b[i] = "12";
1032 else if (a[i] == "D") b[i] = "13";
1033 else if (a[i] == "E") b[i] = "14";
1034 else if (a[i] == "F") b[i] = "15";
1035 else b[i] = a[i];
1036 s1 = s1 + Convert.ToDouble(b[i]) * Math.Pow(16, j);
1037 i--;//chieu dai chuoi giam
1038 j++;//so mu 16 tang
1039
1040 }
1041 // DOI TU DEC SANG BIN
1042
1043 Int32 n = Convert.ToInt32(s1);
1044 Int32 t = 0;
1045 double d, s2 = 0;
1046 while (n > 0)
1047 {
1048 d = n % 2;
1049 n = n / 2;
1050 s2 = s2 + d * Math.Pow(10, t);//lay gia tri bin
1051 t++;
1052 }
1053
1054 /* ############## doi phan le ############# */
1055
1056 // DOI TU HEX SANG DEC
1057
1058 string n2 = P[1];
1059 string[] A;
1060 string[] B;
1061 A = new string[100];
1062 B = new string[100];
1063 Int32 l = n2.Length - 1;// i bang chieu dai chuoi
1064 double s3 = 0;
1065 while (l >= 0)
1066 {
1067 A[l] = n2.Substring(l, 1);// cat chuoi co do dai bang 1
1068 if (A[l] == "A") B[l] = "10";
1069 else if (A[l] == "B") B[l] = "11";
1070 else if (A[l] == "C") B[l] = "12";
1071 else if (A[l] == "D") B[l] = "13";
1072 else if (A[l] == "E") B[l] = "14";
1073 else if (A[l] == "F") B[l] = "15";
1074 else B[l] = A[l];
1075 s3 = s3 + Convert.ToDouble(B[l]) * Math.Pow(16, (-1) * (l + 1));
1076 l--;//chieu dai chuoi giam
1077 }
1078
1079 // DOI TU DEC SANG BIN
1080
1081 string s4 = "";
1082 double g = s3;
1083 Int32 w;
1084 while (g != 0)
1085 {
1086 g = g * 2;
1087 w = Convert.ToInt32(g);//phan nguyen co lam tron
1088 if (w <= g)//lam tron < g
1089 {
1090 s4 = s4+ Convert.ToString(w);
1091 g = g - w;
1092 }
1093 else //lam tron > g
1094 {
1095 s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
1096 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
1097 }
1098 }
1099 textBox2.Text = Convert.ToString(s2)+'.'+Convert.ToString(s4);
1100
1101 }
1102 catch
1103 {
1104 //DOI TU HEX SANG DEC
1105
1106 string n = textBox1.Text;
1107 string[] a;
1108 string[] b;
1109 a = new string[100];
1110 b = new string[100];
1111 Int32 i = n.Length - 1;// i bang chieu dai chuoi
1112 double s = 0, j = 0;
1113 while (i >= 0)
1114 {
1115 a[i] = n.Substring(i, 1);//cat chuoi, co chieu dai bag 1
1116 if (a[i] == "A") b[i] = "10";
1117 else if (a[i] == "B") b[i] = "11";
1118 else if (a[i] == "C") b[i] = "12";
1119 else if (a[i] == "D") b[i] = "13";
1120 else if (a[i] == "E") b[i] = "14";
1121 else if (a[i] == "F") b[i] = "15";
1122 else b[i] = a[i];
1123 s = s + Convert.ToDouble(b[i]) * Math.Pow(16, j);
1124 i--;
1125 j++;
1126
1127 }
1128 //DOI TU DEC SANG BIN
1129
1130 Int32 m = Convert.ToInt32(s);
1131 double t = 0, l = 0, q;
1132 while (m > 0)
1133 {
1134 q = m % 2;
1135 m = m / 2;
1136 t = t + q * Math.Pow(10, l);
1137 l++;
1138 }
1139 textBox2.Text = Convert.ToString(t);
1140 }
1141 }
1142
1143 /* ================================= ĐỔI TỪ HEX SANG OCT ===================================== */
1144
1145 private void button12_Click(object sender, EventArgs e)
1146 {
1147 try
1148 {
1149 string c = textBox1.Text;
1150 string[] P = c.Split('.');
1151
1152 /* ############## doi phan nguyen ############# */
1153
1154 //DOI TU HEX SANG DEC
1155
1156 string n1=P[0];
1157 string[] a;
1158 string[] b;
1159 a = new string[100];
1160 b = new string[100];
1161 Int32 i = n1.Length - 1;// i bang chieu dai chuoi
1162 double s1 = 0, j = 0;
1163 while (i >= 0)
1164 {
1165 a[i] = n1.Substring(i, 1);// cat chuoi co do dai bang 1
1166 if (a[i] == "A") b[i] = "10";
1167 else if (a[i] == "B") b[i] = "11";
1168 else if (a[i] == "C") b[i] = "12";
1169 else if (a[i] == "D") b[i] = "13";
1170 else if (a[i] == "E") b[i] = "14";
1171 else if (a[i] == "F") b[i] = "15";
1172 else b[i] = a[i];
1173 s1 = s1 + Convert.ToDouble(b[i]) * Math.Pow(16, j);
1174 i--;//chieu dai chuoi giam
1175 j++;//so mu 16 tang
1176 }
1177
1178 //DOI TU DEC SANG OCT
1179
1180 Int32 n = Convert.ToInt32(s1);
1181 Int32 t = 0;
1182 double d, s2 = 0;
1183 while (n > 0)
1184 {
1185 d = n % 8;
1186 n = n / 8;
1187 s2 = s2 + d * Math.Pow(10, t);//lay gia tri bin
1188 t++;
1189 };
1190
1191 /* ############## doi phan le ############# */
1192
1193 // DOI TU HEX SANG DEC
1194
1195 string n2 = P[1];
1196 string[] A;
1197 string[] B;
1198 A = new string[100];
1199 B = new string[100];
1200 Int32 l = n2.Length - 1;// i bang chieu dai chuoi
1201 double s3 = 0;
1202 while (l >= 0)
1203 {
1204 A[l] = n2.Substring(l, 1);// cat chuoi co do dai bang 1
1205 if (A[l] == "A") B[l] = "10";
1206 else if (A[l] == "B") B[l] = "11";
1207 else if (A[l] == "C") B[l] = "12";
1208 else if (A[l] == "D") B[l] = "13";
1209 else if (A[l] == "E") B[l] = "14";
1210 else if (A[l] == "F") B[l] = "15";
1211 else B[l] = A[l];
1212 s3= s3 + Convert.ToDouble(B[l]) * Math.Pow(16, (-1) * (l + 1));
1213 l--;//chieu dai chuoi giam
1214 }
1215
1216 // DOI TU DEC SANG OCT
1217
1218 string s4 = "";
1219 double g = s3;
1220
1221 Int32 w;
1222 while (g != 0)//vong lap ket thuc khi phan le =0
1223 {
1224 g = g * 8;
1225 w = Convert.ToInt32(g);//phan nguyen co lam tron
1226 if (w <= g)//lam tron < g
1227 {
1228 s4 = s4 + Convert.ToString(w);
1229 g = g - w;
1230 }
1231 else //lam tron > g
1232 {
1233 s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
1234 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
1235 }
1236 };
1237 textBox2.Text = Convert.ToString(s2) + '.' + Convert.ToString(s4);
1238 }
1239
1240 catch
1241 {
1242 //DOI TU HEX SANG DEC
1243
1244 string n = textBox1.Text;
1245 string[] a;
1246 string[] b;
1247 a = new string[100];
1248 b = new string[100];
1249 Int32 i = n.Length - 1;
1250 double s = 0, j = 0;
1251 while (i >= 0)
1252 {
1253 a[i] = n.Substring(i, 1);
1254 if (a[i] == "A") b[i] = "10";
1255 else if (a[i] == "B") b[i] = "11";
1256 else if (a[i] == "C") b[i] = "12";
1257 else if (a[i] == "D") b[i] = "13";
1258 else if (a[i] == "E") b[i] = "14";
1259 else if (a[i] == "F") b[i] = "15";
1260 else b[i] = a[i];
1261 s = s + Convert.ToDouble(b[i]) * Math.Pow(16, j);
1262 i--;
1263 j++;
1264
1265 }
1266 textBox2.Text = Convert.ToString(s);
1267
1268 //DOI TU DEC SANG OCT
1269
1270 Int32 m = Convert.ToInt32(s);//chuyen hex s vua tinh thanh kieu so nguyen
1271 double t = 0, l = 0, q;
1272 while (m > 0)
1273 {
1274 q = m % 8;
1275 m = m / 8;
1276 t = t + q * Math.Pow(10, l);
1277 l++;
1278 }
1279 textBox2.Text = Convert.ToString(t);
1280 }
1281 }
1282
1283 private void button13_Click(object sender, EventArgs e)
1284 {
1285 MessageBox.Show("Niên luận 1 học kì 2 niên khóa 2008-2009\rĐề tài: Chuyển đổi cơ số\rDo Phan Thị Tươi thực hiện\rMSSV: 1071496\rGVHD: Thái Minh Tuấn", "Thông tin chương trình", MessageBoxButtons.OK, MessageBoxIcon.Information);
1286 }
1287
1288
1289 }
1290 }
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace WindowsFormsApplication1
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 private void label1_Click(object sender, EventArgs e)
20 {
21
22 }
23
24 /* ======================== ĐỔI TỪ DEC SANG BIN =================================== */
25
26 private void button1_Click(object sender, EventArgs e)
27 {
28 try
29 {
30 string a = textBox1.Text;
31 string[] b = a.Split('.'); //cat chuoi
32
33 /* ############## Doi phan nguyen ############*/
34
35 Int32 n = Convert.ToInt32(b[0]);
36 Int32 i = 0;
37 double d, s1 = 0;
38 while (n > 0)
39 {
40 d = n % 2;
41 n = n / 2;
42 s1 = s1 + d * Math.Pow(10, i);//lay gia tri bin
43 i++;
44 };
45
46 /* ############## Doi phan le ############*/
47
48 string f = "0." + b[1];
49 string s2 = "";
50 double g = Convert.ToDouble(f);
51 Int32 w;
52 while (g != 0)
53 {
54 g = g * 2;
55 w = Convert.ToInt32(g);//phan nguyen co lam tron
56 if (w <= g)//lam tron < g
57 {
58 s2 = s2 + Convert.ToString(w);
59 g = g - w;
60 }
61 else //lam tron > g
62 {
63 s2 = s2 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
64 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
65 }
66
67 };
68 textBox2.Text = Convert.ToString(s1) + "." + s2;
69 }
70
71 catch
72 {
73 Int32 n = Int32.Parse(textBox1.Text);//chuyen chuoi trong textBox1 thanh kieu so nguyen
74 double s = 0, i = 0, k;// s phai la kieu double vi chua ham mu
75 while (n > 0)
76 {
77 k = n % 2;
78 n = n / 2;
79 s = s + k * Math.Pow(10, i);// dao nguoc so k vua tinh thanh bin can tim
80 i++;
81 };
82 string b = Convert.ToString(s);//chuyen s ve kieu string
83 textBox2.Text = b;
84 }
85 }
86
87 /* ================================ ĐỔI TỪ DEC SANG OCT ==================================== */
88
89 private void button2_Click(object sender, EventArgs e)
90 {
91 try
92 {
93 string a = textBox1.Text;
94 string[] P = a.Split('.'); //cat chuoi
95 Int32 n = Convert.ToInt32(P[0]);
96
97 /* ############## Doi phan nguyen ############ */
98
99 Int32 i = 0;
100 double d, s1 = 0;
101 while (n > 0)
102 {
103 d = n % 8;
104 n = n / 8;
105 s1 = s1 + d * Math.Pow(10, i);//lay gia tri bin
106 i++;
107 };
108
109 /* ########## Doi phan le ########### */
110
111 string f = "0." + P[1];
112 string s2 = "";
113 double g = Convert.ToDouble(f);
114
115 Int32 w;
116 while (g != 0)//vong lap ket thuc khi phan le =0
117 {
118 g = g * 8;
119 w = Convert.ToInt32(g);//phan nguyen co lam tron
120 if (w <= g)//lam tron < g
121 {
122 s2 = s2 + Convert.ToString(w);
123 g = g - w;
124 }
125 else //lam tron > g
126 {
127 s2 = s2 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
128 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
129 }
130
131 };
132 textBox2.Text = Convert.ToString(s1) + "." + s2;
133
134 }
135 catch
136 {
137 Int32 n = Int32.Parse(textBox1.Text);//chuyen chuoi trong textBox1 thanh kieu so nguyen
138 double s = 0, i = 0, k;// s phai la kieu double vi chua ham mu
139 while (n > 0)
140 {
141 k = n % 8;
142 n = n / 8;
143 s = s + k * Math.Pow(10, i);// dao nguoc so k vua tinh thanh oct can tim
144 i++;
145 };
146 string b = Convert.ToString(s);//chuyen s ve kieu string
147 textBox2.Text = b;
148 }
149
150
151 }
152
153 /* ==================================== ĐỔI TỪ DEC SANG HEX ====================================== */
154
155 private void button3_Click(object sender, EventArgs e)
156 {
157 try
158 {
159 string m = textBox1.Text;
160 string[] P = m.Split('.'); //cat chuoi
161
162 /* ########### Doi phan nguyen ########### */
163
164 Int32 n1 = Convert.ToInt32(P[0]);
165 string[] A; // vi he thap luc phan la chuoi
166 A = new string[100];
167 string s1 = "";
168
169 Int32 i = 0;
170 while (n1 > 0)
171 {
172
173 A[i] = Convert.ToString(n1 % 16); // vi a[i] thuoc kieu string
174 n1 = n1 / 16;
175 i++;
176 };
177
178 //in ra he thap luc phan
179 for (Int32 j = i - 1; j >= 0; j--)// lay j giam de dc he thap luc phan can tim
180 {
181 if (A[j] == "10") A[j] = "A";//a[j] la kieu chuoi
182 if (A[j] == "11") A[j] = "B";
183 if (A[j] == "12") A[j] = "C";
184 if (A[j] == "13") A[j] = "D";
185 if (A[j] == "14") A[j] = "E";
186 if (A[j] == "15") A[j] = "F";
187
188 s1 = s1 + A[j];
189 }
190
191 textBox2.Text = s1;
192
193 /* ########### Doi phan le ########### */
194
195 string f = "0." + P[1];
196 string s2 = "";
197 double g = Convert.ToDouble(f);
198
199 Int64 w;
200
201 while (g != 0)//vong lap ket thuc khi phan le =0
202 {
203 g = g * 16;
204 w = Convert.ToInt64(g);//phan nguyen co lam tron
205 if (w <= g)//lam tron < g
206 {
207
208 if (w == 10) s2 = s2 + "A";
209 else if (w == 11) s2 = s2 + "B";
210 else if (w == 12) s2 = s2 + "C";
211 else if (w == 13) s2 = s2 + "D";
212 else if (w == 14) s2 = s2 + "E";
213 else if (w == 15) s2 = s2 + "F";
214 else s2 = s2 + Convert.ToString(w);
215 g = g - w;
216
217 }
218 else //lam tron > g
219 {
220 if (w-1 == 10) s2 = s2 + "A";
221 else if (w-1 == 11) s2 = s2 + "B";
222 else if (w-1 == 12) s2 = s2 + "C";
223 else if (w-1 == 13) s2 = s2 + "D";
224 else if (w-1 == 14) s2 = s2 + "E";
225 else if (w - 1 == 15) s2 = s2 + "F";
226 else s2 = s2 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
227 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
228
229 }
230 textBox2.Text = Convert.ToString(s1) + "." + s2;
231 };
232
233 }
234
235 catch
236
237 {
238 string[] a; // vi he thap luc phan la chuoi
239 a = new string[50];
240 string b = "";
241
242 Int32 n = Int32.Parse(textBox1.Text);
243 Int32 i = 0;
244 while (n > 0)
245 {
246
247 a[i] = Convert.ToString(n % 16); // vi a[i] thuoc kieu string
248 n = n / 16;
249 i++;
250 };
251
252 //in ra he thap luc phan
253 for (Int32 j = i - 1; j >= 0; j--)// lay j giam de dc he thap luc phan can tim
254 {
255 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
256 if (a[j] == "11") a[j] = "B";
257 if (a[j] == "12") a[j] = "C";
258 if (a[j] == "13") a[j] = "D";
259 if (a[j] == "14") a[j] = "E";
260 if (a[j] == "15") a[j] = "F";
261
262 b = b + a[j];
263 }
264
265 textBox2.Text = b;
266 }
267
268 }
269
270 /* ================================= ĐỔI TỪ BIN SANG DEC ========================================= */
271
272 private void button4_Click(object sender, EventArgs e)
273 {
274 try
275 {
276
277 string a = textBox1.Text;
278 string[] P = a.Split('.');
279
280 /* ############# doi phan nguyen ############### */
281
282 Int32 n1 = Convert.ToInt32(P[0]);
283 double s1 = 0, i = 0, k = 0;
284 while (n1 > 0)
285 {
286 k = n1 % 10; // lay ra phan tu cuoi cung cua bin
287 n1 = n1 / 10;
288 s1 = s1 + k * Math.Pow(2, i);
289 i++;
290 };
291
292
293 /* ############## Doi phan le ############## */
294
295 string n2 = P[1];//phan sau dau cham
296 Int32 j = n2.Length;//dem chieu dai chuoi
297
298 Int32 m = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
299 double s2 = 0;
300 Int32 t = 0;
301 while (j > 0)
302 {
303 t = m % 10;
304 m = m / 10;
305 s2 = s2 + t * Math.Pow(2, (-1) * j);
306 j--;
307
308 }
309 double z = s1 + s2;
310 textBox2.Text = Convert.ToString(z);
311 }
312
313 catch
314 {
315 Int32 n = Int32.Parse(textBox1.Text);
316 double s = 0, i = 0, k = 0;
317 while (n > 0)
318 {
319 k = n % 10; // lay ra phan tu cuoi cung cua bin
320 n = n / 10;
321 s = s + k * Math.Pow(2, i);
322 i++;
323 };
324 string b = Convert.ToString(s);
325 textBox2.Text = b;
326 }
327
328 }
329
330 /* ==================================== ĐỔI TỪ BIN SANG OCT ======================================= */
331
332 private void button5_Click(object sender, EventArgs e)
333 {
334 try
335 {
336 string a = textBox1.Text;
337 string[] P = a.Split('.');
338
339 /* ############ doi phan nguyen ############ */
340
341 // DOI TU BIN SANG DEC
342
343 Int32 n1 = Convert.ToInt32(P[0]);
344 double s1 = 0, i = 0, k = 0;
345 while (n1 > 0)
346 {
347 k = n1 % 10; // lay ra phan tu cuoi cung cua bin
348 n1 = n1 / 10;
349 s1 = s1 + k * Math.Pow(2, i);
350 i++;
351 };
352
353 //DOI TU DEC SANG OCT
354
355 Int32 n = Convert.ToInt32(s1);
356 double s2 = 0,l=0;// s phai la kieu double vi chua ham mu
357 while (n > 0)
358 {
359 k = n % 8;
360 n = n / 8;
361 s2 = s2 + k * Math.Pow(10, l);// dao nguoc so k vua tinh thanh oct can tim
362 l++;
363 };
364
365 /* ############ doi phan le ############ */
366
367 // DOI TU BIN SANG DEC
368
369 string n2 = P[1];//phan sau dau cham
370 Int32 j = n2.Length;//dem chieu dai chuoi
371
372 Int32 m = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
373 double s3 = 0;
374 Int32 t = 0;
375 while (j > 0)
376 {
377 t = m % 10;
378 m = m / 10;
379 s3 = s3 + t * Math.Pow(2, (-1) * j);
380 j--;
381
382 }
383 //DOI TU DEC SANG OCT
384
385 string s4 = "";
386 double g = s3;
387 Int32 w;
388 while (g != 0)//vong lap ket thuc khi phan le =0
389 {
390 g = g * 8;
391 w = Convert.ToInt32(g);//phan nguyen co lam tron
392 if (w <= g)//lam tron < g
393 {
394 s4 = s4 + Convert.ToString(w);
395 g = g - w;
396 }
397 else //lam tron > g
398 {
399 s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
400 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
401 }
402 };
403 textBox2.Text = Convert.ToString(s2)+ '.'+s4;
404 }
405 catch
406 {
407 //DOI TU BIN SANG DEC
408 string a = textBox1.Text;
409 Int32 n1 = Convert.ToInt32(a);
410 double s1 = 0, i = 0, k = 0;
411 while (n1 > 0)
412 {
413 k = n1 % 10; // lay ra phan tu cuoi cung cua bin
414 n1 = n1 / 10;
415 s1 = s1 + k * Math.Pow(2, i);
416 i++;
417 };
418
419 //DOI TU DEC SANG OCT
420
421 Int32 n = Convert.ToInt32(s1);
422 double s2 = 0, l = 0;// s phai la kieu double vi chua ham mu
423 while (n > 0)
424 {
425 k = n % 8;
426 n = n / 8;
427 s2 = s2 + k * Math.Pow(10, l);// dao nguoc so k vua tinh thanh oct can tim
428 l++;
429 };
430 textBox2.Text = Convert.ToString(s2);
431 }
432 }
433
434 /* ================================== ĐỔI TỪ BIN SANG HEX ========================================== */
435
436 private void button6_Click(object sender, EventArgs e)
437 {
438 try
439 {
440 string c = textBox1.Text;
441 string[] P = c.Split('.');
442
443 /* ############## doi phan nguyen ############# */
444
445 // DOI TU BIN SANG DEC
446
447 Int32 n = Convert.ToInt32(P[0]);
448 double s1 = 0, i = 0, k = 0;
449 while (n > 0)
450 {
451 k = n % 10; //lay ra phan tu cuoi cung cua bin
452 n = n / 10;
453 s1 = s1 + k * Math.Pow(2,i);
454 i++;
455 };
456
457 // DOI TU DEC SANG HEX
458
459 string[] a;
460 a = new string[50];
461 string s2 = "";
462
463 /* lay so thap phan s vua tim doi thanh kieu so nguyen
464 roi chuyen thanh he thap luc phan */
465
466 Int32 m = Convert.ToInt32(s1);
467 int q= 0;
468 while (m > 0)
469 {
470
471 a[q] = Convert.ToString(m % 16); // vi a[q] thuoc kieu string
472 m = m / 16;
473 q++;
474 };
475 //in ra he thap luc phan
476 for (Int32 j = q - 1; j >= 0; j--)
477 {
478 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
479 if (a[j] == "11") a[j] = "B";
480 if (a[j] == "12") a[j] = "C";
481 if (a[j] == "13") a[j] = "D";
482 if (a[j] == "14") a[j] = "E";
483 if (a[j] == "15") a[j] = "F";
484
485 s2 = s2+ a[j];
486 }
487 /* ################### doi phan le ################ */
488
489 // DOI TU BIN SANG DEC
490
491 string n2 = P[1];//phan sau dau cham
492 Int32 l = n2.Length;//dem chieu dai chuoi
493
494 Int32 h = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
495 double s3 = 0;
496 Int32 t = 0;
497 while (l > 0)
498 {
499 t = h % 10;
500 h = h / 10;
501 s3 = s3 + t * Math.Pow(2, (-1) * l);
502 l--;
503
504 }
505 // DOI TU DEC SANG HEX
506
507 string s4 = "";
508 double g = s3;
509
510 Int64 w;
511
512 while (g != 0)//vong lap ket thuc khi phan le =0
513 {
514 g = g * 16;
515 w = Convert.ToInt64(g);//phan nguyen co lam tron
516 if (w <= g)//lam tron < g
517 {
518
519 if (w == 10) s4 = s4 + "A";
520 else if (w == 11) s4 = s4 + "B";
521 else if (w == 12) s4 = s4 + "C";
522 else if (w == 13) s4 = s4 + "D";
523 else if (w == 14) s4 = s4 + "E";
524 else if (w == 15) s4 = s4 + "F";
525 else s4 = s4 + Convert.ToString(w);
526 g = g - w;
527
528 }
529 else //lam tron > g
530 {
531 if (w-1 == 10) s4 = s4 + "A";
532 else if (w-1 == 11) s4 = s4 + "B";
533 else if (w-1 == 12) s4 = s4 + "C";
534 else if (w-1 == 13) s4 = s4 + "D";
535 else if (w-1 == 14) s4 = s4 + "E";
536 else if (w - 1 == 15) s4 = s4 + "F";
537 else s4= s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
538 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
539 }
540 }
541 textBox2.Text = Convert.ToString(s2) + "." + s4;
542
543 }
544 catch
545 {
546 // DOI TU BIN SANG DEC
547
548 Int32 n = Int32.Parse(textBox1.Text);
549 double s = 0, i = 0, k = 0;
550 while (n > 0)
551 {
552 k = n % 10; //lay ra phan tu cuoi cung cua bin
553 n = n / 10;
554 s = s + k * Math.Pow(2,i);
555 i++;
556 };
557
558 // DOI TU DEC SANG HEX
559
560 string[] a;
561 a = new string[50];
562 string b = "";
563
564 /* lay so thap phan s vua tim doi thanh kieu so nguyen
565 roi chuyen thanh he thap luc phan */
566
567 Int32 m = Convert.ToInt32(s);
568 int q= 0;
569 while (m > 0)
570 {
571
572 a[q] = Convert.ToString(m % 16); // vi a[q] thuoc kieu string
573 m = m / 16;
574 q++;
575 };
576 //in ra he thap luc phan
577 for (Int32 j = q - 1; j >= 0; j--)
578 {
579 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
580 if (a[j] == "11") a[j] = "B";
581 if (a[j] == "12") a[j] = "C";
582 if (a[j] == "13") a[j] = "D";
583 if (a[j] == "14") a[j] = "E";
584 if (a[j] == "15") a[j] = "F";
585
586 b = b + a[j];
587 }
588
589 textBox2.Text = b;
590 }
591 }
592
593 /* ========================================= ĐỔI TỪ OCT SANG DEC ============================================= */
594
595 private void button7_Click(object sender, EventArgs e)
596 {
597
598 try
599 {
600
601 string a = textBox1.Text;
602 string[] P = a.Split('.');
603
604 /* ############## doi phan nguyen ############ */
605
606 Int32 n1 = Convert.ToInt32(P[0]);
607 double s1 = 0, i = 0, k = 0;
608 while (n1 > 0)
609 {
610 k = n1 % 10; // lay ra phan tu cuoi cung cua bin
611 n1 = n1 / 10;
612 s1 = s1 + k * Math.Pow(8, i);
613 i++;
614 };
615
616
617 /* ############ Doi phan le ##################*/
618
619 string n2 = P[1];//phan sau dau cham
620 Int32 j = n2.Length;//dem chieu dai chuoi
621
622 Int32 m = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
623 double s2 = 0;
624 Int32 t = 0;
625 while (j > 0)
626 {
627 t = m % 10;
628 m = m / 10;
629 s2 = s2 + t * Math.Pow(8, (-1) * j);
630 j--;
631
632 }
633 double z = s1 + s2;
634 textBox2.Text = Convert.ToString(z);
635 }
636
637 catch
638 {
639
640
641
642 Int32 n = Int32.Parse(textBox1.Text);
643 double s = 0, i = 0, k = 0;
644 while (n > 0)
645 {
646 k = n % 10;// lay ra phan tu cuoi cung cua oct
647 n = n / 10;
648 s = s + k * Math.Pow(8, i);
649 i++;
650 };
651 string b = Convert.ToString(s);
652 textBox2.Text = b;
653 }
654 }
655
656
657 /* =================================== ĐỔI TỪ OCT SANG BIN ========================================*/
658
659 private void button8_Click(object sender, EventArgs e)
660 {
661 try
662 {
663 string a = textBox1.Text;
664 string[] P = a.Split('.');
665
666 /* ############## doi phan nguyen ###########*/
667
668 //DOI TU OCT SANG DEC
669
670 Int32 n = Convert.ToInt32(P[0]);
671 double s1 = 0, i = 0, k = 0, s2 = 0;
672 while (n > 0)
673 {
674 k = n % 10; // lay ra phan tu cuoi cung cua oct
675 n = n / 10;
676 s1 = s1 + k * Math.Pow(8, i);
677 i++;
678 };
679
680 //DOI TU DEC SANG BIN
681
682 Int32 q = Convert.ToInt32(s1);
683 i = 0;
684 while (q > 0)
685 {
686 k = q % 2;
687 q = q / 2;
688 s2 = s2 + k * Math.Pow(10, i); //doi bin k vua tinh ra nguoc lai de duoc bin can tim
689 i++;
690 };
691
692 /* ############## doi phan le ###########*/
693
694 //DOI TU OCT SANG DEC
695
696 string n2 = P[1];//phan sau dau cham
697 Int32 j = n2.Length;//dem chieu dai chuoi
698
699 Int32 m = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
700 double s3 = 0;
701 Int32 t = 0;
702 while (j > 0)
703 {
704 t = m % 10;
705 m = m / 10;
706 s3 = s3 + t * Math.Pow(8, (-1) * j);
707 j--;
708
709 }
710
711 //DOI TU DEC SANG BIN
712
713 string s4 = "";
714 double g = s3;
715 Int32 w;
716 while (g != 0)
717 {
718 g = g * 2;
719 w = Convert.ToInt32(g);//phan nguyen co lam tron
720 if (w <= g)//lam tron < g
721 {
722 s4 = s4 + Convert.ToString(w);
723 g = g - w;
724 }
725 else //lam tron > g
726 {
727 s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
728 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
729 }
730 };
731 textBox2.Text = Convert.ToString(s2) + '.' + s4;
732 }
733 catch
734 {
735 // DOI TU OCT SANG DEC
736
737 Int32 n = Int32.Parse(textBox1.Text);
738 double s1 = 0, i = 0, k = 0, s2 = 0;
739 while (n > 0)
740 {
741 k = n % 10; // lay ra phan tu cuoi cung cua oct
742 n = n / 10;
743 s1 = s1 + k * Math.Pow(8, i);
744 i++;
745 };
746
747 //DOI TU DEC SANG BIN
748
749 Int32 q = Convert.ToInt32(s1);
750 i = 0;
751 while (q > 0)
752 {
753 k = q % 2;
754 q = q / 2;
755 s2 = s2 + k * Math.Pow(10, i); //doi bin k vua tinh ra nguoc lai de duoc bin can tim
756 i++;
757 };
758 textBox2.Text = Convert.ToString(s2);
759 }
760 }
761
762 /* ===================================== ĐỔI TỪ OCT SANG HEX =============================================== */
763
764 private void button9_Click(object sender, EventArgs e)
765 {
766 try
767 {
768 string b = textBox1.Text;
769 string[] P = b.Split('.');
770
771 /* ############## doi phan nguyen ############ */
772
773 //DOI TU OCT SANG DEC
774
775 Int32 n = Convert.ToInt32(P[0]);
776 double s1 = 0, i = 0, k = 0;
777 while (n > 0)
778 {
779 k = n % 10;//lay ra phan tu cuoi cung cua oct
780 n = n / 10;
781 s1 = s1 + k * Math.Pow(8, i);
782 i++;
783 }
784
785 //DOI TU DEC SANG HEX
786
787 string[] a;
788 a = new string[100];
789 string s2 = "";
790
791 /* lay so thap phan s1 vua tim doi thanh kieu so nguyen
792 roi chuyen thanh he thap luc phan */
793
794 Int32 m = Convert.ToInt32(s1);
795 Int32 q = 0;
796 while (m > 0)
797 {
798 a[q] = Convert.ToString(m % 16);
799 m = m / 16;
800 q++;
801 }
802 //in he thap luc phan
803 for (Int32 j = q - 1; j >= 0; j--)
804 {
805 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
806 if (a[j] == "11") a[j] = "B";
807 if (a[j] == "12") a[j] = "C";
808 if (a[j] == "13") a[j] = "D";
809 if (a[j] == "14") a[j] = "E";
810 if (a[j] == "15") a[j] = "F";
811
812 s2 = s2 + a[j];
813 }
814 /* ############ Doi phan le ##################*/
815
816 //DOI TU OCT SANG DEC
817
818 string n2 = P[1];//phan sau dau cham
819 Int32 l = n2.Length;//dem chieu dai chuoi
820
821 Int32 m1 = Convert.ToInt32(n2);//chuyen kieu chuoi sang kieu int
822 double s3 = 0;
823 Int32 t = 0;
824 while (l > 0)
825 {
826 t = m1 % 10;
827 m1 = m1 / 10;
828 s3 = s3 + t * Math.Pow(8, (-1) * l);
829 l--;
830 }
831 //DOI TU DEC SANG HEC
832
833 string s4 = "";
834 double g = s3;
835
836 Int64 w;
837
838 while (g != 0)//vong lap ket thuc khi phan le =0
839 {
840 g = g * 16;
841 w = Convert.ToInt64(g);//phan nguyen co lam tron
842 if (w <= g)//lam tron < g
843 {
844
845 if (w == 10) s4 = s4 + "A";
846 else if (w == 11) s4 = s4 + "B";
847 else if (w == 12) s4 = s4 + "C";
848 else if (w == 13) s4 = s4 + "D";
849 else if (w == 14) s4 = s4 + "E";
850 else if (w == 15) s4 = s4 + "F";
851 else s4 = s4 + Convert.ToString(w);
852 g = g - w;
853
854 }
855 else //lam tron > g
856 {
857 if (w - 1 == 10) s4 = s4 + "A";
858 else if (w - 1 == 11) s4 = s4 + "B";
859 else if (w - 1 == 12) s4 = s4 + "C";
860 else if (w - 1 == 13) s4 = s4 + "D";
861 else if (w - 1 == 14) s4 = s4 + "E";
862 else if (w - 1 == 15) s4 = s4 + "F";
863 else s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
864 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
865
866 }
867 }
868 textBox2.Text = Convert.ToString(s2) + "." + s4;
869 }
870
871 catch
872 {
873 //DOI TU OCT SANG DEC
874
875 Int32 n = Int32.Parse(textBox1.Text);
876 double s = 0, i = 0, k = 0;
877 while (n > 0)
878 {
879 k = n % 10;//lay ra phan tu cuoi cung cua oct
880 n = n / 10;
881 s = s + k * Math.Pow(8, i);
882 i++;
883 }
884
885 //DOI TU DEC SANG HEC
886
887 string[] a;
888 a = new string[50];
889 string b = "";
890
891 /* lay so thap phan s vua tim doi thanh kieu so nguyen
892 roi chuyen thanh he thap luc phan */
893
894 Int32 m = Convert.ToInt32(s);
895 Int32 q = 0;
896 while (m > 0)
897 {
898 a[q] = Convert.ToString(m % 16);
899 m = m / 16;
900 q++;
901 }
902 //in he thap luc phan
903 for (Int32 j = q - 1; j >= 0; j--)
904 {
905 if (a[j] == "10") a[j] = "A";//a[j] la kieu chuoi
906 if (a[j] == "11") a[j] = "B";
907 if (a[j] == "12") a[j] = "C";
908 if (a[j] == "13") a[j] = "D";
909 if (a[j] == "14") a[j] = "E";
910 if (a[j] == "15") a[j] = "F";
911
912 b = b + a[j];
913 }
914
915 textBox2.Text = b;
916 }
917
918 }
919 /* ================================== ĐỔI TỪ HEX SANG DEC ========================================= */
920
921 private void button10_Click(object sender, EventArgs e)
922 {
923 try
924 {
925 string c = textBox1.Text;
926 string[] P = c.Split('.');
927
928 /* ############## doi phan nguyen ############# */
929
930 string n1=P[0];
931 string[] a;
932 string[] b;
933 a = new string[100];
934 b = new string[100];
935 Int32 i = n1.Length - 1;// i bang chieu dai chuoi
936 double s1 = 0, j = 0;
937 while (i >= 0)
938 {
939 a[i] = n1.Substring(i,1);// cat chuoi co do dai bang 1
940 if (a[i] == "A") b[i] = "10";
941 else if (a[i] == "B") b[i] = "11";
942 else if (a[i] == "C") b[i] = "12";
943 else if (a[i] == "D") b[i] = "13";
944 else if (a[i] == "E") b[i] = "14";
945 else if (a[i] == "F") b[i] = "15";
946 else b[i] = a[i];
947 s1 = s1 + Convert.ToDouble(b[i]) * Math.Pow(16, j);
948 i--;//chieu dai chuoi giam
949 j++;//so mu 16 tang
950
951 }
952 /* ############## doi phan le ############# */
953
954 string n2 = P[1];
955 string[] A;
956 string[] B;
957 A = new string[100];
958 B= new string[100];
959 Int32 l = n2.Length - 1;// l bang chieu dai chuoi
960 double s2 = 0;
961 while (l >= 0)
962 {
963 A[l] = n2.Substring(l, 1);// cat chuoi co do dai bang 1
964 if (A[l] == "A") B[l] = "10";
965 else if (A[l] == "B") B[l] = "11";
966 else if (A[l] == "C") B[l] = "12";
967 else if (A[l] == "D") B[l] = "13";
968 else if (A[l] == "E") B[l] = "14";
969 else if (A[l] == "F") B[l] = "15";
970 else B[l] = A[l];
971 s2 = s2 + Convert.ToDouble(B[l]) * Math.Pow(16, (-1) * (l + 1));
972 l--;//chieu dai chuoi giam
973 }
974 double s3 = s1 + s2;
975 textBox2.Text = Convert.ToString(s3);
976 }
977
978 catch
979 {
980 string n = textBox1.Text;
981 string[] a;
982 string[] b;
983 a = new string[100];
984 b = new string[100];
985 Int32 i = n.Length - 1;// i bang chieu dai chuoi
986 double s = 0, j = 0;
987 while (i >= 0)
988 {
989 a[i] = n.Substring(i, 1);// cat chuoi co do dai bang 1
990 if (a[i] == "A") b[i] = "10";
991 else if (a[i] == "B") b[i] = "11";
992 else if (a[i] == "C") b[i] = "12";
993 else if (a[i] == "D") b[i] = "13";
994 else if (a[i] == "E") b[i] = "14";
995 else if (a[i] == "F") b[i] = "15";
996 else b[i] = a[i];
997 s = s + Convert.ToDouble(b[i]) * Math.Pow(16, j);
998 i--;//chieu dai chuoi giam
999 j++;//so mu 16 tang
1000
1001 }
1002 textBox2.Text = Convert.ToString(s);
1003 }
1004 }
1005
1006 /* ======================================= ĐỔI TỪ HEX SANG BIN ===================================== */
1007
1008 private void button11_Click(object sender, EventArgs e)
1009 {
1010 try
1011 {
1012 string c = textBox1.Text;
1013 string[] P = c.Split('.');
1014
1015 /* ############## doi phan nguyen ############# */
1016
1017 // DOI TU HEX SANG DEC
1018
1019 string n1 = P[0];
1020 string[] a;
1021 string[] b;
1022 a = new string[100];
1023 b = new string[100];
1024 Int32 i = n1.Length - 1;// i bang chieu dai chuoi
1025 double s1 = 0, j = 0;
1026 while (i >= 0)
1027 {
1028 a[i] = n1.Substring(i, 1);// cat chuoi co do dai bang 1
1029 if (a[i] == "A") b[i] = "10";
1030 else if (a[i] == "B") b[i] = "11";
1031 else if (a[i] == "C") b[i] = "12";
1032 else if (a[i] == "D") b[i] = "13";
1033 else if (a[i] == "E") b[i] = "14";
1034 else if (a[i] == "F") b[i] = "15";
1035 else b[i] = a[i];
1036 s1 = s1 + Convert.ToDouble(b[i]) * Math.Pow(16, j);
1037 i--;//chieu dai chuoi giam
1038 j++;//so mu 16 tang
1039
1040 }
1041 // DOI TU DEC SANG BIN
1042
1043 Int32 n = Convert.ToInt32(s1);
1044 Int32 t = 0;
1045 double d, s2 = 0;
1046 while (n > 0)
1047 {
1048 d = n % 2;
1049 n = n / 2;
1050 s2 = s2 + d * Math.Pow(10, t);//lay gia tri bin
1051 t++;
1052 }
1053
1054 /* ############## doi phan le ############# */
1055
1056 // DOI TU HEX SANG DEC
1057
1058 string n2 = P[1];
1059 string[] A;
1060 string[] B;
1061 A = new string[100];
1062 B = new string[100];
1063 Int32 l = n2.Length - 1;// i bang chieu dai chuoi
1064 double s3 = 0;
1065 while (l >= 0)
1066 {
1067 A[l] = n2.Substring(l, 1);// cat chuoi co do dai bang 1
1068 if (A[l] == "A") B[l] = "10";
1069 else if (A[l] == "B") B[l] = "11";
1070 else if (A[l] == "C") B[l] = "12";
1071 else if (A[l] == "D") B[l] = "13";
1072 else if (A[l] == "E") B[l] = "14";
1073 else if (A[l] == "F") B[l] = "15";
1074 else B[l] = A[l];
1075 s3 = s3 + Convert.ToDouble(B[l]) * Math.Pow(16, (-1) * (l + 1));
1076 l--;//chieu dai chuoi giam
1077 }
1078
1079 // DOI TU DEC SANG BIN
1080
1081 string s4 = "";
1082 double g = s3;
1083 Int32 w;
1084 while (g != 0)
1085 {
1086 g = g * 2;
1087 w = Convert.ToInt32(g);//phan nguyen co lam tron
1088 if (w <= g)//lam tron < g
1089 {
1090 s4 = s4+ Convert.ToString(w);
1091 g = g - w;
1092 }
1093 else //lam tron > g
1094 {
1095 s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
1096 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
1097 }
1098 }
1099 textBox2.Text = Convert.ToString(s2)+'.'+Convert.ToString(s4);
1100
1101 }
1102 catch
1103 {
1104 //DOI TU HEX SANG DEC
1105
1106 string n = textBox1.Text;
1107 string[] a;
1108 string[] b;
1109 a = new string[100];
1110 b = new string[100];
1111 Int32 i = n.Length - 1;// i bang chieu dai chuoi
1112 double s = 0, j = 0;
1113 while (i >= 0)
1114 {
1115 a[i] = n.Substring(i, 1);//cat chuoi, co chieu dai bag 1
1116 if (a[i] == "A") b[i] = "10";
1117 else if (a[i] == "B") b[i] = "11";
1118 else if (a[i] == "C") b[i] = "12";
1119 else if (a[i] == "D") b[i] = "13";
1120 else if (a[i] == "E") b[i] = "14";
1121 else if (a[i] == "F") b[i] = "15";
1122 else b[i] = a[i];
1123 s = s + Convert.ToDouble(b[i]) * Math.Pow(16, j);
1124 i--;
1125 j++;
1126
1127 }
1128 //DOI TU DEC SANG BIN
1129
1130 Int32 m = Convert.ToInt32(s);
1131 double t = 0, l = 0, q;
1132 while (m > 0)
1133 {
1134 q = m % 2;
1135 m = m / 2;
1136 t = t + q * Math.Pow(10, l);
1137 l++;
1138 }
1139 textBox2.Text = Convert.ToString(t);
1140 }
1141 }
1142
1143 /* ================================= ĐỔI TỪ HEX SANG OCT ===================================== */
1144
1145 private void button12_Click(object sender, EventArgs e)
1146 {
1147 try
1148 {
1149 string c = textBox1.Text;
1150 string[] P = c.Split('.');
1151
1152 /* ############## doi phan nguyen ############# */
1153
1154 //DOI TU HEX SANG DEC
1155
1156 string n1=P[0];
1157 string[] a;
1158 string[] b;
1159 a = new string[100];
1160 b = new string[100];
1161 Int32 i = n1.Length - 1;// i bang chieu dai chuoi
1162 double s1 = 0, j = 0;
1163 while (i >= 0)
1164 {
1165 a[i] = n1.Substring(i, 1);// cat chuoi co do dai bang 1
1166 if (a[i] == "A") b[i] = "10";
1167 else if (a[i] == "B") b[i] = "11";
1168 else if (a[i] == "C") b[i] = "12";
1169 else if (a[i] == "D") b[i] = "13";
1170 else if (a[i] == "E") b[i] = "14";
1171 else if (a[i] == "F") b[i] = "15";
1172 else b[i] = a[i];
1173 s1 = s1 + Convert.ToDouble(b[i]) * Math.Pow(16, j);
1174 i--;//chieu dai chuoi giam
1175 j++;//so mu 16 tang
1176 }
1177
1178 //DOI TU DEC SANG OCT
1179
1180 Int32 n = Convert.ToInt32(s1);
1181 Int32 t = 0;
1182 double d, s2 = 0;
1183 while (n > 0)
1184 {
1185 d = n % 8;
1186 n = n / 8;
1187 s2 = s2 + d * Math.Pow(10, t);//lay gia tri bin
1188 t++;
1189 };
1190
1191 /* ############## doi phan le ############# */
1192
1193 // DOI TU HEX SANG DEC
1194
1195 string n2 = P[1];
1196 string[] A;
1197 string[] B;
1198 A = new string[100];
1199 B = new string[100];
1200 Int32 l = n2.Length - 1;// i bang chieu dai chuoi
1201 double s3 = 0;
1202 while (l >= 0)
1203 {
1204 A[l] = n2.Substring(l, 1);// cat chuoi co do dai bang 1
1205 if (A[l] == "A") B[l] = "10";
1206 else if (A[l] == "B") B[l] = "11";
1207 else if (A[l] == "C") B[l] = "12";
1208 else if (A[l] == "D") B[l] = "13";
1209 else if (A[l] == "E") B[l] = "14";
1210 else if (A[l] == "F") B[l] = "15";
1211 else B[l] = A[l];
1212 s3= s3 + Convert.ToDouble(B[l]) * Math.Pow(16, (-1) * (l + 1));
1213 l--;//chieu dai chuoi giam
1214 }
1215
1216 // DOI TU DEC SANG OCT
1217
1218 string s4 = "";
1219 double g = s3;
1220
1221 Int32 w;
1222 while (g != 0)//vong lap ket thuc khi phan le =0
1223 {
1224 g = g * 8;
1225 w = Convert.ToInt32(g);//phan nguyen co lam tron
1226 if (w <= g)//lam tron < g
1227 {
1228 s4 = s4 + Convert.ToString(w);
1229 g = g - w;
1230 }
1231 else //lam tron > g
1232 {
1233 s4 = s4 + Convert.ToString(w - 1);//lam tron tang len 1 nen khi in phan nguyen phai tru 1
1234 g = g + 1 - w;//g phai tang len 1 roi moi tru cho phan nguyen
1235 }
1236 };
1237 textBox2.Text = Convert.ToString(s2) + '.' + Convert.ToString(s4);
1238 }
1239
1240 catch
1241 {
1242 //DOI TU HEX SANG DEC
1243
1244 string n = textBox1.Text;
1245 string[] a;
1246 string[] b;
1247 a = new string[100];
1248 b = new string[100];
1249 Int32 i = n.Length - 1;
1250 double s = 0, j = 0;
1251 while (i >= 0)
1252 {
1253 a[i] = n.Substring(i, 1);
1254 if (a[i] == "A") b[i] = "10";
1255 else if (a[i] == "B") b[i] = "11";
1256 else if (a[i] == "C") b[i] = "12";
1257 else if (a[i] == "D") b[i] = "13";
1258 else if (a[i] == "E") b[i] = "14";
1259 else if (a[i] == "F") b[i] = "15";
1260 else b[i] = a[i];
1261 s = s + Convert.ToDouble(b[i]) * Math.Pow(16, j);
1262 i--;
1263 j++;
1264
1265 }
1266 textBox2.Text = Convert.ToString(s);
1267
1268 //DOI TU DEC SANG OCT
1269
1270 Int32 m = Convert.ToInt32(s);//chuyen hex s vua tinh thanh kieu so nguyen
1271 double t = 0, l = 0, q;
1272 while (m > 0)
1273 {
1274 q = m % 8;
1275 m = m / 8;
1276 t = t + q * Math.Pow(10, l);
1277 l++;
1278 }
1279 textBox2.Text = Convert.ToString(t);
1280 }
1281 }
1282
1283 private void button13_Click(object sender, EventArgs e)
1284 {
1285 MessageBox.Show("Niên luận 1 học kì 2 niên khóa 2008-2009\rĐề tài: Chuyển đổi cơ số\rDo Phan Thị Tươi thực hiện\rMSSV: 1071496\rGVHD: Thái Minh Tuấn", "Thông tin chương trình", MessageBoxButtons.OK, MessageBoxIcon.Information);
1286 }
1287
1288
1289 }
1290 }